# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=irrlicht
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.8.4
pkgrel=1
pkgdesc="An open source high performance realtime 3D graphics engine (mingw-w64)"
arch=('any')
url="https://irrlicht.sourceforge.io/"
license=("ZLIB")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "dos2unix")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('strip' 'staticlibs')
source=("https://downloads.sourceforge.net/irrlicht/irrlicht-${pkgver}.zip")
sha256sums=('f42b280bc608e545b820206fe2a999c55f290de5c7509a02bdbeeccc1bf9e433')

prepare() {
  cd "${srcdir}"
  find . -type f -exec dos2unix {} \;
}

build() {
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" "${srcdir}/build-${MINGW_CHOST}-static"
  cp -r "${srcdir}/irrlicht-${pkgver}/"* "${srcdir}/build-${MINGW_CHOST}-shared"
  cp -r "${srcdir}/irrlicht-${pkgver}/"* "${srcdir}/build-${MINGW_CHOST}-static"
  cd "${srcdir}/build-${MINGW_CHOST}-shared/source/Irrlicht"
  if [ ${MINGW_CHOST} = "x86_64-w64-mingw32" ]; then
    sed -i "s,-ld3dx9d,,g" Makefile
  fi
  CXX=${MINGW_CHOST}-g++ CC=${MINGW_CHOST}-gcc AR=ar make NDEBUG=1 sharedlib_win32
  cd "${srcdir}/build-${MINGW_CHOST}-static/source/Irrlicht"
  CXX=${MINGW_CHOST}-g++ CC=${MINGW_CHOST}-gcc AR=ar make NDEBUG=1 staticlib_win32
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  install -Dm644 "bin/Win32-gcc/Irrlicht.dll" "${pkgdir}${MINGW_PREFIX}/bin/Irrlicht.dll"
  install -Dm644 "lib/Win32-gcc/libIrrlicht.a" "${pkgdir}${MINGW_PREFIX}/lib/libIrrlicht.dll.a"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/include/irrlicht"
  cp "include/"*.h "${pkgdir}${MINGW_PREFIX}/include/irrlicht"

  cd "${srcdir}/build-${MINGW_CHOST}-static"
  install -m644 "lib/Win32-gcc/libIrrlicht.a" "${pkgdir}${MINGW_PREFIX}/lib/"
}
